package com.haofengsoft.scce.adapter.quotations;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

/**
 * 为ViewPager添加布局（Fragment），绑定和处理fragments和viewpager之间的逻辑关系
 */
public class QuotationsMenuAdapter extends FragmentStatePagerAdapter {

	private List<QuotationsMenuModel> modelList;
	private FragmentManager fragmentManager;

	public QuotationsMenuAdapter(FragmentManager fragmentManager, List<QuotationsMenuModel> modelList) {
		super(fragmentManager);
		this.modelList = modelList;
		this.fragmentManager = fragmentManager;
	}

	public String getPageTitle(int position) {
		return modelList.get(position).getTitle();
	}

	public void setFragments(List<QuotationsMenuModel> modelList) {
		FragmentTransaction ft = fragmentManager.beginTransaction();
		for (QuotationsMenuModel model : modelList) {
			ft.remove(model.getFragment());
		}
		ft.commit();
		fragmentManager.executePendingTransactions();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return modelList.size();
	}

	@Override
	public Fragment getItem(int position) {
		//
		return modelList.get(position).getFragment();
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

}
